package com.yw.pms.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.yw.pms.dao.ModuleDao;
import com.yw.pms.dao.common.Page;
import com.yw.pms.dao.entity.Module;
import com.yw.pms.utils.CommonResp;

@Service
public class ModuleService {

	private final static Logger logger = LoggerFactory.getLogger(ModuleService.class);

	@Resource
	ModuleDao moduleDao;

	public CommonResp<Boolean> addModule(Module module) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			moduleDao.insert(module);
			commonResp.success();
			logger.info("操作员编号【{}】新增模块【{}】成功", module.getOperatorId(), module);
		} catch (Exception e) {
			logger.error("新增模块发生异常", e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<Module>> queryModuleList(Module module) {
		module.setPage(new Page(module.getPageNo()));
		CommonResp<List<Module>> commonResp = new CommonResp<List<Module>>();
		try {
			List<Module> listPage = moduleDao.listPage(module);
			commonResp.success();
			commonResp.setData(listPage);
			Map<String,Object> extraData = Maps.newHashMapWithExpectedSize(1);
			extraData.put("page", module.getPage());
			commonResp.setExtraData(extraData);
			logger.info("操作员ID【{}】查询模块信息【{}】成功",module.getOperatorId(),commonResp);
		} catch (Exception e) {
			logger.error("分页查询模块列表发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<Module>> queryModuleListByProductId(Module module) {
		CommonResp<List<Module>> commonResp = new CommonResp<List<Module>>();
		try {
			List<Module> listPage = moduleDao.listPage(module);
			commonResp.success();
			commonResp.setData(listPage);
			logger.info("查询模块信息【{}】成功",commonResp);
		} catch (Exception e) {
			logger.error("查询模块列表发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> deleteModule(String moduleId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			Integer id = Integer.valueOf(moduleId);
			moduleDao.delete(id);
			commonResp.success();
			logger.info("删除模块【{}】成功",moduleId);
		} catch (Exception e) {
			logger.error("删除模块发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Module> get(String moduleId){
		CommonResp<Module> commonResp = new CommonResp<Module>();
		try {
			Module module = moduleDao.getById(Integer.valueOf(moduleId));
			commonResp.success();
			commonResp.setData(module);
			logger.info("查询模块【{}】成功",moduleId);
		} catch (Exception e) {
			logger.error("查询模块信息系统异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> isModuleNameUniq(String moduleName, String productId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			Module module = new Module();
			module.setModuleName(moduleName);
			module.setProductId(Integer.valueOf(productId));
			Module module2 = moduleDao.get(module);
			if(module2 == null) {
				commonResp.success();
			} else {
				commonResp.fail();
			}
			logger.info("校验模块名称【{}】重复结果【{}】（成功为不存在，失败为存在）",moduleName,commonResp.getMsg());
			return commonResp;
		} catch (Exception e) {
			logger.error("校验模块名称发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> updateModule(Module module) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			moduleDao.update(module);
			commonResp.success();
			logger.info("更新模块【{}】成功",module.getModuleName());
		} catch (Exception e) {
			logger.error("更新模块发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
}
